﻿using System.Linq;

namespace Nxg.Blog.Core.Commands.Accounts {
    public class ChangeProfileCommand {
        public string AuthorId { get; set; }

        public string NewEmail { get; set; }

        public string NewDisplayName { get; set; }
    }

    public class ChangeProfileCommandInvoker : ICommandInvoker<ChangeProfileCommand, CommandResult> {
        private readonly DataContext _db;

        public ChangeProfileCommandInvoker (DataContext db) {
            _db = db;
        }

        public CommandResult Execute (ChangeProfileCommand command) {
            var author = _db.Author.FirstOrDefault (x => x.Id.ToString () == command.AuthorId);
            if (author == null)
                return new CommandResult ("用户信息不存在");
            author.DisplayName = command.NewDisplayName;
            author.Email = command.NewEmail;
            _db.SaveChanges ();
            return CommandResult.SuccessResult;
        }
    }
}